﻿using QAU.DFS.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

namespace QAU.DFS.ImageService2
{
    /// <summary>
    /// FileUp 的摘要说明
    /// </summary>
    public class FileUp : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int serverId = int.Parse(context.Request["serverId"]);
            string ext = context.Request["ext"];
            string dir = "/Image/" + DateTime.Now.Day + "/";
            Directory.CreateDirectory(Path.GetDirectoryName(context.Request.MapPath(dir)));
            string newFileName = Guid.NewGuid().ToString();
            string fullDir = dir + newFileName + ext;
            using (FileStream fileStream = File.OpenWrite(context.Request.MapPath(fullDir)))
            {
                context.Request.InputStream.CopyTo(fileStream);
                ImageInfo imageInfo = new ImageInfo();
                imageInfo.ImageName = fullDir;
                imageInfo.ImageServerId = serverId;
                ImageServerEntities db = new ImageServerEntities();
                db.ImageInfo.Add(imageInfo);
                db.SaveChanges();

            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}